package slick.path2glory.tutorial1;

import java.awt.Font;
import java.util.ArrayList;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class MainMenuState extends BasicGameState {

	int stateID = 0;

	Image background = null;
	Image startGameOption = null;
	Image exitOption = null;

	public MainMenuState(int stateID) {
		this.stateID = stateID;
	}

	@Override
	public int getID() {
		return stateID;
	}

	public void init(GameContainer arg0, StateBasedGame sb)
			throws SlickException {

		Image menuOptions = new Image("data/menuoptions.png");
		startGameOption = menuOptions.getSubImage(0, 0, 377, 71);
		background = new Image("data/menu.jpg");
		exitOption = menuOptions.getSubImage(0, 71, 377, 71);

	}

	public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
			throws SlickException {

		background.draw(0, 0);

		startGameOption.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (startGameOption.getWidth() / 2),
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (startGameOption.getHeight() / 2), 1);

		exitOption.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (startGameOption.getWidth() / 2),
				SlickBasicGame.SCREEN_HEIGHT / 2 + 80
						- (startGameOption.getHeight() / 2), 1);

	}

	public void update(GameContainer gc, StateBasedGame sb, int delta)
			throws SlickException {

		Input input = gc.getInput();
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();

		boolean insideStartGame = false;
		boolean insideExit = false;

		if (insideStartGame(mouseX, mouseY)) {
			insideStartGame = true;
		} else if (insideExit(mouseX, mouseY)) {
			insideExit = true;
		}

		if (insideStartGame) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				sb.enterState(SlickBasicGame.CHARACTERSELECTSTATE);
			}
		}

		if (insideExit) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				gc.exit();
			}
		}
	}

	public boolean insideStartGame(int mouseX, int mouseY) {

		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (startGameOption.getWidth() / 2) && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + startGameOption.getWidth() / 2)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2
						- (startGameOption.getHeight() / 2) && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2 + startGameOption.getHeight() / 2)) {
			return true;
		} else {
			return false;
		}

	}

	public boolean insideExit(int mouseX, int mouseY) {

		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (exitOption.getWidth() / 2) && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + exitOption.getWidth() / 2)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2 + 80
						- (exitOption.getHeight() / 2) && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2 + 80 + exitOption.getHeight() / 2)) {
			return true;
		} else {
			return false;
		}

	}

}